package com.play.ballen.me.ui

import android.content.Context
import android.content.Intent
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.databinding.ActivityTeamCenterBinding
import com.play.ballen.me.adapter.TeamInviteRecordAdapter
import com.play.ballen.me.dialog.BottomSheetDialogUtils
import com.play.ballen.me.entity.TeamLevelInfo
import com.play.ballen.me.vm.TeamCenterVM
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.utils.ResourceUtil
import com.yuven.baselib.widget.MultiStateView

class TeamCenterActivity : BaseStdTitleActivity<TeamCenterVM, ActivityTeamCenterBinding>() {

    private val adapter: TeamInviteRecordAdapter by lazy {
        TeamInviteRecordAdapter(arrayListOf())
    }
    private var selectPercent = false
    private var selectId = ""

    private val teamPercentDialogUtils by lazy {
        BottomSheetDialogUtils<TeamLevelInfo>(this).apply {
            itemCallback = { select ->
                selectId = select.title
                viewModel.page = 1
                viewModel.team(selectId, true)
                dataBinding.tvPercent.text = select.getItemString()
                selectPercent = true
            }
        }
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, TeamCenterActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun viewModelClass(): Class<TeamCenterVM> {
        return TeamCenterVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_team_center
    }

    override fun initObserver() {
        super.initObserver()

        viewModel.actionGetFailure.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()
            if (viewModel.page == 1) {
                dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_ERROR
            } else {
                ToastUtils.show(ResourceUtil.getString(R.string.load_fail))
            }
        }

        viewModel.teamInfo.observe(this) {
            it.run {
                dataBinding.tvCount.text = recommend
                dataBinding.tvPerformance.text =
                    ResourceUtil.getString(R.string.price_unit_bmt, total)
                dataBinding.tvReword.text =
                    ResourceUtil.getString(R.string.price_unit_bmt, reward)

                dataBinding.refreshLayout.finishRefresh()
                dataBinding.refreshLayout.finishLoadMore()
                if (!list.isNullOrEmpty()) {
                    if (viewModel.page == 1) {
                        adapter.setList(list)

                        dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_CONTENT
                    } else {
                        adapter.addData(list)
                    }
                    viewModel.page++
                } else {
                    //no data
                    if (viewModel.page == 1) {
                        dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_EMPTY
                    } else {
                        ToastUtils.show(ResourceUtil.getString(R.string.load_end))
                    }
                }

                //首次默认显示第一个
                if (!selectPercent && !percents.isNullOrEmpty()) {
                    dataBinding.tvPercent.text = "${percents[0]}%"
                }
            }
        }
    }

    override fun initView() {
        super.initView()
        dataBinding.msvLayout.findViewById<Button>(R.id.btnTryAgain).setOnClickListener {
            dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
            viewModel.team(selectId)
        }

        dataBinding.llPercent.setOnSingleClickListener tag@{
            viewModel.teamInfo.value?.run {
                if (!percents.isNullOrEmpty()) {
                    val list = arrayListOf<TeamLevelInfo>()
                    percents.forEach {
                        list.add(TeamLevelInfo(it))
                    }
                    teamPercentDialogUtils.show(list)
                }
            }
        }
    }

    override fun initData() {
        dataBinding.refreshLayout.setEnableLoadMore(true)
        dataBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.page = 1
                viewModel.team(selectId)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.team(selectId)
            }
        })
        dataBinding.rv.layoutManager = LinearLayoutManager(mContext)
        dataBinding.rv.adapter = adapter

        dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
        viewModel.page = 1
        viewModel.team(selectId)
    }

    override fun initIntent(intent: Intent?) {
    }

    override fun getToolBarTitle(): String = ResourceUtil.getString(R.string.team_title)
}